<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Tree;
use app\api\model\goods\Goods as GoodsModel;
use app\api\model\hospital\Hospital as HospitalModel;

/**
 * 项目接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取项目列表
     */
    public function list(){
        $page = $this->request->get('page',1);
        $limit = $this->request->get('limit',10);
        $goods_category_id = $this->request->get('goods_category_id',0);
        $where = [
            'price'=>['>',0],
        ];
        if ($this->request->get('title')){
            $where['title'] = ['like','%'.$this->request->get('title').'%'];
        }
        $goods_category_ids = [];
        if($goods_category_id){
            $goods_category_ids = (new \app\api\model\goods\GoodsCategory())->where('pid',$goods_category_id)->column('id');
            if(empty($goods_category_ids)){
                $goods_category_ids = [$goods_category_id];
            }
        }

        $data = GoodsModel::where($where)->where(function($query) use ($goods_category_ids){
            if(count($goods_category_ids)){
                foreach ($goods_category_ids as $key=>$goods_category_id){
                    if($key == 0){
                        $query->where("FIND_IN_SET({$goods_category_id}, goods_category_id)");
                    }else{
                        $query->whereOr("FIND_IN_SET({$goods_category_id}, goods_category_id)");
                    }
                }
            }
        })->paginate($limit, false, ['page' => $page]);
        $this->success('',$data);
    }

    /**
     * 获取项目详情
     */
    public function detail(){
        if (!$this->request->get('id')){
            return $this->error('参数错误');
        }
        $id = $this->request->get('id');
        $detail = GoodsModel::with(['biaobenRongqi','biaobenType'])->find($id)->toArray();
        $correlation_list = [];
        if($detail['type'] == 10){
            $correlation_list = [$detail];
        }else{
            $correlation_list = (new \app\api\model\goods\GoodsDetails())->alias('goods_detail')->field('goods.*')->join('goods','goods.id = goods_detail.goods_id')->where(['goods_master_id'=>$id])->select();
        }

        $detail['correlation_list'] = $correlation_list;
        $this->success('',$detail);
    }

    /**
     * 获取项目分类tree商品
     */
    public function categoryTreeGoodsList(){
        $tree = Tree::instance();
        $list = (new \app\api\model\goods\GoodsCategory())->order('weigh desc,id desc')->select();
        foreach ($list as &$v){
            $v['goodslist'] = \app\api\model\goods\Goods::where("FIND_IN_SET({$v['id']}, goods_category_id)")->select();
        }
        $tree->init($list, 'pid');
        $this->success('',$tree->getTreeArray(0));
    }

    /** 
     * 获取项目分类tree
     */
    public function categoryTree(){
        $tree = Tree::instance();
        $tree->init(collection((new \app\api\model\goods\GoodsCategory())->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $this->success('',$tree->getTreeArray(0));
    }

    /** 
     * 获取项目分类
     */
    public function category(){
        $pid = $this->request->get('pid',0);
        $list = (new \app\api\model\goods\GoodsCategory())->where('pid',$pid)->order('weigh desc,id desc')->select();
        $this->success('',$list);
    }

    /**
     * 获取套餐列表
     */
    public function packageList(){
        $page = $this->request->post('page',1);
        $limit = $this->request->post('limit',10);
        $goods_active_id = $this->request->post('goods_active_id','');
        $only_active = $this->request->post('only_active','');
        $where = [];
        if ($this->request->post('title')){
            $where[] = ['title','like','%'.$this->request->post('title').'%'];
        }

        $data = \app\api\model\goods\GoodsPackage::where($where)->where(function($query) use ($goods_active_id,$only_active){
            if($goods_active_id){
                $query->where("FIND_IN_SET({$goods_active_id}, goods_active_ids)");
            }
            if($only_active==1){
                $query->where('goods_active_ids','not null');
            }
        })->where('deletetime',null)->paginate($limit, false, ['page' => $page]);
        $this->success('',$data);
        
    }

    /**
     * 活动列表
     */
    public function activeList(){
        $data = \app\api\model\goods\GoodsActive::where('deletetime',null)->select();
        $this->success('',$data);
    }

    
}
